package com.miku.project.job;

import com.miku.mikuapicommon.model.entity.DailyCheckIn;
import com.miku.project.service.DailyCheckInService;
import com.miku.project.utils.RedissonLockUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现登录用户每日签到
 */
@Component
public class DailyCheckJob {
    @Resource
    private DailyCheckInService dailyCheckInService;

    @Resource
    private RedissonLockUtil redissonLockUtil;

    /**
     * 每天晚上12点批量清除签到列表
     */
    @Scheduled(cron="0 0 0 * * *")
    public void clearCheckInList(){
        redissonLockUtil.redissonDistributedLocks("clearCheckInList",()->{
            //每批删除的数据量
            int batchSize=1000;
            //是否还有需要数据需要删除
            boolean hasMaorDate=true;
            while (hasMaorDate){
                //分批查询数据
                List<DailyCheckIn> list = dailyCheckInService.list();
                if (list.isEmpty()){
                    //没有数据了,退出循环
                    hasMaorDate=false;
                    continue;
                }else {
                    //批量删除数据
                    dailyCheckInService.removeByIds(list.stream().map(DailyCheckIn::getId).collect(Collectors.toList()));
                }
            }
        });
    }
}
